/**
 * 1.生成节点和端口洞察显示框架界面。<br>
 * 2.根据节点和端口提取对应端口的洞察显示界面<br>
 * 
 * @param nodeType 所要展示的节点的类型 :'data_input'
 * */
function MineViewingInsightNodePortController(showMaskOpt, oView,filterCondition,insightResult,portsArray, callback) {
	oView.html("");
	var oDivTabs = $('<div id="tempo-mine-input-view-tabs">').appendTo(oView);
	var parentEle = $('<div id="tempo-mine-input-view-view" class="tempo-mine-insight-table">').appendTo(oView);
	//refreshInsight();
	//insightResult洞察数据
	//加载左侧节点端口列表
	loadInsightInfo(showMaskOpt, insightResult,filterCondition,oDivTabs,parentEle,portsArray, callback);
}
window.MineViewingInsightNodePortController = MineViewingInsightNodePortController;
window.dataResultType={proceduce:"proceduce",result:"result"} //用于数据统计类型
window.loadFrameInsightPageTypeArray = ['evaluation','classification','regression','association', 'comprehensiveevaluation', 'timeseries','textmining','recommendation'];//加载节点内部编写的洞察js文件
window.loadFrameInsightPageNodeIdArray = ['loopsampledata'];
window.processTypes = ['preprocess','datamine','dm_model','minechart','dm_control','dm_extend','dm_deeplearning'];

/** 
 * 加载左侧节点端口列表
 * @param nodeType 所要展示的节点的类型
 * @param insightResult 当前流程所有需要展示的洞察数据
 * @private
 * */
window.loadInsightInfo = function(showMaskOpt, insightResult,filterCondition,oDivTabs,parentEle, portsArray, callback) {
	var isFirst = true;
	var inputInfo;
	oDivTabs.html("");
	if(processTypes.indexOf(filterCondition) != -1){
		var oDiv = $("<div/>",{"class":"tempo-mine-viewing-nodestype"}).appendTo(oDivTabs);
		var nodeSelect = $('<select class="nodeselect">').appendTo(oDiv);
		nodeSelect.append($("<option value='preprocess' id='tempo-mine-viewing-dataprocess'>"+TempoUtils.locale("mine.dataprocess")+"</option>"))
		.append($("<option value='datamine' id='tempo-mine-viewing-dataprocess-alg'>"+TempoUtils.locale("mine.mine_alg")+"</option>"))
		.append($("<option value='dm_model'  id='tempo-mine-viewing-dataprocess-model'>"+TempoUtils.locale("mine.mine_model")+"</option>"))
		.append($("<option value='minechart' id='tempo-mine-viewing-dataprocess-pic'>"+TempoUtils.locale("mine.mine_graphic")+"</option>"))
		.append($("<option value='dm_control' id='tempo-mine-viewing-dataprocess-control'>"+TempoUtils.locale("mine.mine_control")+"</option>"));
		nodeSelect.val(filterCondition);
		nodeSelect.selectpick({
			container: oDiv,
			onSelect: function(value,text){
				if(callback && $.isFunction(callback)){
					callback(value);
				}
			}
		});
	}
	result = generateCategoryData(insightResult,filterCondition,portsArray);
	var nodes = []
	var nodeType2Category = { dm_data : "data_input" };
	for (var i = 0; i < result.length; i++) {
		var node = result[i];
		var curInfo = node.info ? JSON.parse(node.info) : {};
		var nodeType = nodeType2Category[node.nodeType] ? nodeType2Category[node.nodeType] : node.nodeType;
		if(nodeType.indexOf(filterCondition) != -1 || 
				curInfo.locType == filterCondition || 
				node.locType == filterCondition ||
				curInfo.type == filterCondition){
			nodes.push(node);
		}
	}

	for (var m = 0; m < nodes.length; m++) {
		var dataNode = $('<div class="nodeitem"><lable title='+nodes[m].nodeLabel+'>'+nodes[m].nodeLabel +'</lable></div>').appendTo(oDivTabs);
		dataNode.unbind('click').bind('click',nodes[m],(function(event) {
			$(this).addClass('viewtabsbackgroundcolor').siblings().removeClass('viewtabsbackgroundcolor');
			var insightResItem = event.data;
			var portType = insightResItem.portType;
			showDataSetSwitchButton(showMaskOpt, parentEle,portType,insightResItem, processTypes.indexOf(filterCondition) > -1 || filterCondition=='result');
		}));
	}
	
	oDivTabs.find('.nodeitem:first').trigger('click');
	
}

/**
 * 根据端口类型返回端口标签
 */
window.getPortLabel = function(insightResItem) {
	var insightData = JSON.parse(insightResItem.info);
	var portType = insightResItem.portType || insightData.portType;
	if (TempoMineUtils.isModel(portType)) {
		return TempoUtils.locale("mine.mine_model");
	} else if (TempoMineUtils.isDataSet(portType)) {
		if (insightResItem.portLabel) {
			return insightResItem.portLabel;
		} else {
			return TempoUtils.locale("mine.mine_dataset");
		}
	} else if (TempoMineUtils.isGraph(portType)) {
		return TempoUtils.locale("mine.mine_graphic");
	}
}

/***
 * 如果洞察节点端口是数据输入集,则显示 原始数据和原始数据统计信息 两个按钮,否则不显示
 * @param oDiv
 * @param nodeType
 */
window.showDataSetSwitchButton = function(showMaskOpt, parentEle,portType,insightResItem, isProcess){
	parentEle.html("");
	var oDivOptions = $("<div/>",{"class":"tempo-mine-viewing-options"}).appendTo(parentEle).css({'height': 36,'overflow': 'auto'});
	var oDivData = $("<div/>").appendTo(parentEle).css('height', parentEle.height()-oDivOptions.height()-30).css('overflow', 'auto');
	showChildrenPort(showMaskOpt, insightResItem, parentEle,portType, oDivOptions, oDivData, isProcess)
	oDivOptions.find('.tempo-mine-viewing-result-style:first').trigger('click');
}

window.showChildrenPort = function(showMaskOpt, insightResItem, parentEle, portType, oDivOptions, oDivData, isProcess){
	if(insightResItem.portChildren){
		if(isNecessaryDescNode(insightResItem)){
			showPortView(showMaskOpt, parentEle,portType,insightResItem, oDivOptions, oDivData);
		}
		var children = insightResItem.portChildren;
		for (var i = 0; i < children.length; i++) {
			var node = children[i];
			showChildrenPort(showMaskOpt, node, parentEle, node.portType, oDivOptions, oDivData, isProcess);
		}
	}else{
		showPortView(showMaskOpt, parentEle,portType,insightResItem, oDivOptions, oDivData, isProcess);
	}
}

window.showPortView = function(showMaskOpt, parentEle,portType,insightResItem, oDivOptions, oDivData, isProcess){
	if(!portType){
		return;
	}
	if(TempoMineUtils.isDataSet(portType)) {
			if(!isProcess){
				oDivOptions.append($("<span class='tempo-mine-viewing-result-style' id='tempo-mine-viewing-rawdata'></span>").data('insightId',insightResItem.id).text((insightResItem.subProcessNodeId ? insightResItem.nodeLabel : '') + TempoUtils.locale("mine.originalData")))
				.append($("<span class='tempo-mine-viewing-result-style' id='tempo-mine-viewing-rawdata-statistics'></span>").data('insightId',insightResItem.id).text((insightResItem.subProcessNodeId ? insightResItem.nodeLabel : '') + TempoUtils.locale("mine.originalDataStatisticsInfo")));
				
				$('#tempo-mine-viewing-rawdata').unbind('click').bind('click', function(event) {
					var insightId = $(this).data('insightId');
					var that = this;
					getPortInsight(insightId, function(insightInfo){
						createResTable(insightInfo,oDivData);
					},oDivData, that)
					
				});
				
				$('#tempo-mine-viewing-rawdata-statistics').unbind('click').bind('click', function(event) {
					var insightId = $(this).data('insightId');
					var that = this;
					getPortInsight(insightId, function(insightInfo){
						createResTableInfo1(insightInfo,oDivData,dataResultType.proceduce);
					},oDivData, that)
				});
			}else if(insightResItem.nodeType == "textmining"){
				$("<span class='tempo-mine-viewing-result-style' id='tempo-mine-viewing-dataset'></span>").data('insightId',insightResItem.id).text((insightResItem.subProcessNodeId ? insightResItem.nodeLabel : '') + insightResItem.portLabel).appendTo(oDivOptions).unbind('click')
				.bind('click',insightResItem, function(event){
					var insightResItem = event.data;
					var insightId = $(this).data('insightId');
					//初始化算法参数界面
					var pageParser = function(parent){
						return {
							init: function(param) {
								param = param || {};
								var values = param.values || {};
								TempoUtils.parserUi(parent, values, $.noop);
							}
						}
					}
					getPortInsight(insightId, function(insightInfo){
						var paramsConfig = MineFlowInfo.getParameters(insightResItem.nodeId);
						$m.loadHtmlSyn(contextPath+'/api/ai/mine/'+ window[insightInfo.type][insightInfo.nodeConfigId].datasetInsightPage,
								oDivData, insightResItem.nodeId, {parameters : paramsConfig, source:oDivData, insight:insightInfo, pageParser: pageParser, nodeId: insightResItem.nodeId});
					},oDivData,this)
				});
			}else{
				$("<span class='tempo-mine-viewing-result-style' id='tempo-mine-viewing-dataset'></span>").data('insightId',insightResItem.id).text((insightResItem.subProcessNodeId ? insightResItem.nodeLabel : '') + getPortLabel(insightResItem)).appendTo(oDivOptions).unbind('click')
					.bind('click', function(event){
						var insightId = $(this).data('insightId');
						getPortInsight(insightId, function(insightInfo){
							var inputPanel = $('<div id="input-container">').appendTo(oDivData).accordionpanel({
								expanded : true,
								title : TempoUtils.locale("mine.originalData")
							});
							createResTable(insightInfo,inputPanel.accordionpanel('getContent'));
							
							var sumPanel = $('<div id="input-container">').appendTo(oDivData).accordionpanel({
								expanded : true,
								title : TempoUtils.locale("mine.originalDataStatisticsInfo")
							});
							createResTableInfo1(insightInfo,sumPanel.accordionpanel('getContent'),dataResultType.proceduce);
							//oDivData.find('table').height((oDivData.height()-180)/2);
						},oDivData, this)
					});
				
			}
			
	} else if (TempoMineUtils.isModel(portType)){
		$("<span class='tempo-mine-viewing-result-style' id='tempo-mine-viewing-model'></span>").data('insightId',insightResItem.id).text((insightResItem.subProcessNodeId ? insightResItem.nodeLabel : '') + getPortLabel(insightResItem)).appendTo(oDivOptions).unbind('click').bind('click',insightResItem, function(event){
			var insightId = $(this).data('insightId');
			var insightResItem = event.data;
			//初始化算法参数界面
			var pageParser = function(parent){
				return {
					init: function(param) {
						param = param || {};
						var values = param.values || {};
						TempoUtils.parserUi(parent, values, $.noop);
					}
				}
			}
			getPortInsight(insightId, function(insightInfo){
				var configParentDiv = $('<div id="tempo-mine-alg-config-page-id" class="tempo-mine-alg-config-page">').appendTo(oDivData);
				if (insightInfo.type == 'datamine') {
					var paramsConfig = MineFlowInfo.getParameters(insightResItem.nodeId);
					$m.loadHtmlSyn(contextPath+'/api/ai/mine/'+ window[insightInfo.type][insightInfo.nodeConfigId].propertyPage,
							configParentDiv, insightResItem.nodeId, {source:configParentDiv,data:{ parameters:paramsConfig, formEditable:false,algNodeIsHidden:true}, dialog: pageParser(configParentDiv)});
					
				} else if (insightInfo.type == 'dm_control'){
					var algNodeInsightInfo = insightInfo.algNodeModelInsightInfo;
					var algParamsConfig = MineFlowInfo.getParameters(insightInfo.algNodeModelInsightInfo.producerNodeId);
					$m.loadHtmlSyn(contextPath+'/api/ai/mine/'+ window['datamine'][insightInfo.algNodeModelInsightInfo.producerNodeTypeId].propertyPage,
							configParentDiv, insightInfo.algNodeModelInsightInfo.producerNodeId, {source:configParentDiv,data:{ parameters:algParamsConfig, formEditable:false,algNodeIsHidden:true}, dialog: pageParser(configParentDiv)});
					var algNodeConfig = insightInfo.algNodeModelInsightInfo.producerNodeCategory;
					if (algNodeConfig == 'clustering') {
						if (window['datamine'][insightInfo.algNodeModelInsightInfo.producerNodeTypeId].frameInsightPage) {
							var paramsConfig = MineFlowInfo.getParameters(insightInfo.algNodeModelInsightInfo.producerNodeId);
							if (paramsConfig) {
								var clusterNodeInsightDiv = $("<div/>").appendTo(oDivData);
								$m.loadHtmlSyn(contextPath+'/api/ai/mine/'+ window['datamine'][insightInfo.algNodeModelInsightInfo.producerNodeTypeId].frameInsightPage,
										clusterNodeInsightDiv, insightInfo.algNodeModelInsightInfo.producerNodeId, {source:clusterNodeInsightDiv, insight:algNodeInsightInfo, parameters:paramsConfig, dialog: pageParser(clusterNodeInsightDiv)});
							}
						}
						showClusterResPic1(algNodeInsightInfo, oDivData);
					} else {
						$m.loadHtmlSyn(contextPath+'/api/ai/mine/'+ window['datamine'][insightInfo.algNodeModelInsightInfo.producerNodeTypeId].frameInsightPage,oDivData, insightInfo.algNodeModelInsightInfo.producerNodeId, {source:oDivData, insight:algNodeInsightInfo, dialog: pageParser(oDivData)});
					}
				} else if (insightInfo.type == 'dm_model') {
					if (insightResItem.nodeType == 'model_io') {
						var paramsConfig = MineFlowInfo.getParameters(insightResItem.nodeId);
						$m.loadHtmlSyn(contextPath+'/api/ai/mine/'+ window[insightInfo.type][insightInfo.nodeConfigId].propertyPage,
								configParentDiv, insightResItem.nodeId, {source:configParentDiv,data:{ parameters:paramsConfig, formEditable:false,algNodeIsHidden:true},dialog: pageParser(configParentDiv)});
						var algNodeInsightInfo = insightInfo.algNodeModelInsightInfo;
						var algParamsConfig = MineFlowInfo.getParameters(insightInfo.algNodeModelInsightInfo.producerNodeId);
						if (!algParamsConfig || $.isEmptyObject(algParamsConfig)) {
							algParamsConfig = TempoUtils.clone($("#"+insightInfo.algNodeModelInsightInfo.producerNodeId).miningnode("getData").parameters);
						}
						$m.loadHtmlSyn(contextPath+'/api/ai/mine/'+ window['datamine'][insightInfo.algNodeModelInsightInfo.producerNodeTypeId].propertyPage,
								configParentDiv, insightInfo.algNodeModelInsightInfo.producerNodeId, {source:configParentDiv,data:{ parameters:algParamsConfig, formEditable:false,algNodeIsHidden:true}, dialog: pageParser(configParentDiv)});
						var algNodeConfig = insightInfo.algNodeModelInsightInfo.producerNodeCategory;
						if (algNodeConfig == 'clustering') {
							if (window['datamine'][insightInfo.algNodeModelInsightInfo.producerNodeTypeId].frameInsightPage) {
								var paramsConfig = MineFlowInfo.getParameters(insightInfo.algNodeModelInsightInfo.producerNodeId);
								if (paramsConfig) {
									var clusterNodeInsightDiv = $("<div/>").appendTo(oDivData);
									$m.loadHtmlSyn(contextPath+'/api/ai/mine/'+ window['datamine'][insightInfo.algNodeModelInsightInfo.producerNodeTypeId].frameInsightPage,
											clusterNodeInsightDiv, insightInfo.algNodeModelInsightInfo.producerNodeId, {source:clusterNodeInsightDiv, insight:algNodeInsightInfo, parameters:paramsConfig, dialog: pageParser(clusterNodeInsightDiv)});
								}
							}
							showClusterResPic1(algNodeInsightInfo, oDivData);
						} else {
							$m.loadHtmlSyn(contextPath+'/api/ai/mine/'+ window['datamine'][insightInfo.algNodeModelInsightInfo.producerNodeTypeId].frameInsightPage,oDivData, insightInfo.algNodeModelInsightInfo.producerNodeId, {source:oDivData, insight:algNodeInsightInfo, dialog: pageParser(oDivData)});
						}
					}
				}
				debugger
				if (insightResItem.nodeType == 'clustering') {
					if (window[insightInfo.type][insightInfo.nodeConfigId].frameInsightPage) {
						var paramsConfig = MineFlowInfo.getParameters(insightResItem.nodeId);
						if (paramsConfig) {
							var clusterNodeInsightDiv = $("<div/>").appendTo(oDivData);
							$m.loadHtmlSyn(contextPath+'/api/ai/mine/'+ window[insightInfo.type][insightInfo.nodeConfigId].frameInsightPage,
									clusterNodeInsightDiv, insightResItem.nodeId, {source:clusterNodeInsightDiv, insight:insightInfo, parameters:paramsConfig, dialog: pageParser(clusterNodeInsightDiv)});
						}
					}
					showClusterResPic1(insightInfo,oDivData);
					oDivData.find('.tempo-mine-viewing-result-style:first').trigger('click');
				} else if (loadFrameInsightPageTypeArray.indexOf(insightResItem.nodeType) != -1) {
					var paramsConfig = MineFlowInfo.getParameters(insightResItem.nodeId);
					if (paramsConfig) {
						$m.loadHtmlSyn(contextPath+'/api/ai/mine/'+ window[insightInfo.type][insightInfo.nodeConfigId].frameInsightPage,oDivData, insightResItem.nodeId, {source:oDivData, insight:insightInfo, parameters:paramsConfig, dialog: pageParser(oDivData)});
					}
				}
			},oDivData,this)
		
		});
	} else if (!TempoMineUtils.isNotDefaultPort(portType)) {
		$("<span class='tempo-mine-viewing-result-style' id='tempo-mine-viewing-node'></span>").data('insightId',insightResItem.id).text((insightResItem.subProcessNodeId ? insightResItem.nodeLabel : '') + "节点").appendTo(oDivOptions).unbind('click').bind('click',insightResItem, function(event){
			var insightResItem = event.data;
			var insightId = $(this).data('insightId');
			getPortInsight(insightId, function(insightInfo){
				$m.loadHtmlSyn(contextPath+'/api/ai/mine/'+ window[insightInfo.type][insightInfo.nodeConfigId].frameInsightPage,
						oDivData, insightResItem.nodeId, {source:oDivData, insight:insightInfo});
			},oDivData,this)
		})
	}else if (TempoMineUtils.isGraph(portType)){
		$("<span class='tempo-mine-viewing-result-style'></span>").data('insightId',insightResItem.id).text((insightResItem.subProcessNodeId ? insightResItem.nodeLabel : '') + getPortLabel(insightResItem)).appendTo(oDivOptions).unbind('click').bind('click',insightResItem, function(event){
			var insightResItem = event.data;
			var insightId = $(this).data('insightId');
			var paramsConfig = MineFlowInfo.getParameters(insightResItem.nodeId);
			getPortInsight(insightId, function(insightInfo){
				$m.loadHtmlSyn(contextPath+'/api/ai/mine/'+ window[insightInfo.type][insightInfo.nodeConfigId].frameInsightPage,
						oDivData, insightResItem.nodeId, {source:oDivData, insight:insightInfo,parameters:paramsConfig});
			},oDivData,this)
		});
	}else {
		var insightResItem = event.data;
		var insightId = $(this).data('insightId');
		getPortInsight(insightId, function(insightInfo){
			var insightInfo = event.data[0];
			var insightResItem = event.data[1];
			$m.loadHtmlSyn(contextPath+'/api/ai/mine/'+ window[insightInfo.type][insightInfo.nodeConfigId].frameInsightPage,
					oDivData, insightResItem.nodeId, {source:oDivData, insight:insightInfo});
		})
	}
	
	
	var getPortInsight = function(id, callback, container, itemSpan){
		$.ajax({
			url:contextPath+"/api/ai/mine/searchInsightInfoById",
			data:{
				id: id
			},
			type:'post',
			success:function(data){
				var insightInfo = data;
				container.empty();
				if(itemSpan){
					if (!$(itemSpan).hasClass('tempo-mine-viewing-result-style-sel')) {
						$(itemSpan).addClass('tempo-mine-viewing-result-style-sel');
					}
					$(itemSpan).siblings().removeClass('tempo-mine-viewing-result-style-sel');
				}
				callback(data);
				showMaskOpt('hide');
			},			
			error:function(){
				showMaskOpt('hide');
			}
		})
		
	}
}